package elevatronic.view.simulation;

import pl.gdan.elsy.tool.Rand;
import elevatronic.models.People;
import elevatronic.utils.Utils;
import elevatronic.view.ImageView;

/**
 * 
 * @author Benjamin CORNEC
 * people view of people model
 */
public class PeopleView {
	
	//people model
	private People people;
	public People getPeople() {
		return people;
	}
	public void setPeople(People people) {
		this.people = people;
	}
	
	private FloorView floorView;
	private ElevatorView elevatorView;
	
	
	//used to know where set the people on the floor, what posX choose
	private int idOnTheFloor;
	public int getIdOnTheFloor() {
		//<if people is in an elevator, we set his position by a negative position>
		if (elevatorView!=null)
			return -1*elevatorView.getElevator().getId();
		//</if people is in an elevator, we set his position by a negative position>
		return idOnTheFloor;
	}
	public void setIdOnTheFloor(int idOnTheFloor) {
		this.idOnTheFloor = idOnTheFloor;
	}
	
	
	//image of the floor
	private ImageView imagePeople;
	public ImageView getImagePeople() {
		return imagePeople;
	}
	public void setImagePeople(ImageView imagePeople) {
		this.imagePeople = imagePeople;
	}
	
	//constructor
	public PeopleView(People people){
		setPeople(people);
		setImagePeople(new ImageView("people"+Rand.i(1, 4),0,0,11,47));
	}
	
	//iteration of the floor
	public void update(int step){
		elevatorView=findElevator();
		floorView=findFloor();
		reLocalisation();
	}

	public ElevatorView findElevator(){
		BuildingView buildingView=BuildingView.getSingleton();
		for(ElevatorView elevatorView:buildingView.getElevatorsView()){
			if (elevatorView.getPeopleView().contains(this)){
				return elevatorView;
			}
		}
		return null;
	}
	
	
	public FloorView findFloor(){
		BuildingView buildingView=BuildingView.getSingleton();
		for(FloorView floorView:buildingView.getFloorsView()){
			if (floorView.getPeopleView().contains(this)){
				return floorView;
			}
		}
		return null;
	}
	
	public double getFloorLocalization(){
		double retour=-1;
		if (floorView!=null){
			retour=floorView.getFloor().getNumber();
		}
		if (elevatorView!=null){
			retour=elevatorView.getFloorLocalization();
		}		
		return retour;
	}
	
	
	/**
	 * This method allow to set X and Y on the screen
	 */
	private void reLocalisation(){
		getImagePeople().setPosX(getPosX());
		getImagePeople().setPosY(Utils.GetPosY(getFloorLocalization(), getImagePeople().getHght()));
	}
	
	public int getPosX(){
		int step=-1;
		int left=0;
		int imgWdth=getImagePeople().getWdth();
		if (floorView!=null){
			//if the people is on the floor
			int nbPeople=floorView.getFloor().getPeopleAtFloor().size();
			step=(FloorView.POS_X_CENTER+FloorView.POS_X)-(nbPeople/2)*imgWdth;
			left=getIdOnTheFloor()*imgWdth;
		} else {
			//so, if the people is in an elevator
			step=ElevatorView.FIRST_ELEVATOR_POS_X;
			left=ElevatorView.SEPARATION_BETWEEN_TWO_ELEVATOR*-1*getIdOnTheFloor();
		}
		
		return step+left;
	}
	
}
